package com.cloudptt.api.models.msg;



import java.util.HashMap;
import java.util.Map;

import com.cloudptt.api.comm.CheckData;
import com.cloudptt.api.product.func.call.CallAttr;
import com.cloudptt.api.product.log.Log;
import com.mcptt.db.model.SessionInfo;

/**
 * SIP消息扩展部分信息
 * @author TP
 *
 */
public class MsgExtStrInfo 
{
	private final static String THIS_FILE = "MsgExtStrInfo";
	
//	public static final int MsgType_Single = 1;
//	public static final int MsgType_Group = 2;
//	public static final int MsgType_Sys = 3;
	
	
//	public static final String MsgType_Desc_Group = "group_msg";
//	public static final String MsgType_Desc_Single = "single_msg";
//	public static final String MsgType_Desc_Sys = "sys_msg";
//
//	
//	public final static String DefauleValue = "";
//	public final static String Key_Sender = "senderId";
//	public final static String Key_Group = "groupId";
//	public final static String Key_MsgType = "msgType";
//	public final static String Key_Len = "len";
//	public final static String Key_Len = "len";
	
	public static final String MsgType_Desc_SDS = "SDS";
	public static final String MsgType_Desc_FD = "FD";
	
	public static final int MsgType_SDS = 1;
	public static final int MsgType_FD = 2;
	
	public static final String SessionType_Desc_O2O = "O2O";
	public static final String SessionType_Desc_O2G = "O2G";
	
	public static final int SessionType_O2O = 1;
	public static final int SessionType_O2G = 2;
	
	
	public final static String Key_Attr = "Attr1";
	public final static String Key_CType = "CType";
	public final static String Key_PNum = "PNum";
	public final static String Key_GNum = "GNum";
	public final static String Key_AlarmNum = "alarmnum";
	
	private int sessionType = SessionInfo.SessionType_Group;
	private String senderId = null;
	
	private String msgTypeStr = null;
	private int msgType = SessionInfo.Msgtype_Txt;
	private Map<String,String> valueMap = null;

	public MsgExtStrInfo(int SessionType,int msgType,String senderId)
	{
		this.sessionType = SessionType;
		this.msgType = msgType;
		this.senderId = senderId;
	}
	
	public MsgExtStrInfo(String extStr)
	{
		Log.d(THIS_FILE, "CallExtStrInfo extStr = " + extStr);
		extStr = extStr.trim();
		if(extStr.startsWith(":"))
		{
			extStr = extStr.substring(1,extStr.length() -1);
		}
		Log.d(THIS_FILE, "CallExtStrInfo extStr = " + extStr);
		String[] strArr = extStr.split(";");
		valueMap = new HashMap<String,String>();
		for(int i = 0; i < strArr.length; i++)
		{
			String value = strArr[i];
			if(!value.contains("="))
			{
				this.msgTypeStr = strArr[i].trim();
			}
			else
			{
				String[] valueArr = value.split("=");
				if(valueArr.length == 2)
				{
					if(valueArr[0] != null
							&& valueArr[1] != null)
					{
						String key = valueArr[0].trim().toLowerCase();
						valueMap.put(key, valueArr[1].trim());
					}
				}
			}
		}
	}
	
	public String getValue(String key)
	{
		key = key.toLowerCase();
		if(valueMap != null
				&& valueMap.get(key) != null)
		{
			return valueMap.get(key);
		}
		return "";
	}
	
	public int getSessionType()
	{
		String value = valueMap.get(Key_Attr.toLowerCase());
		
		if(SessionType_Desc_O2G.toLowerCase().equals(value))
		{
			return SessionInfo.SessionType_Group;
		}
		if(SessionType_Desc_O2O.toLowerCase().equals(value))
		{
			return SessionInfo.SessionType_Single;
		}
		else 
		{
			return SessionInfo.SessionType_Single;
		}
	}
	
	public int getMsgType()
	{
		if(MsgType_Desc_SDS.toLowerCase().equals(msgTypeStr))
		{
			return SessionInfo.Msgtype_Txt;
		}
		else if(MsgType_Desc_FD.toLowerCase().equals(msgTypeStr))
		{
			return SessionInfo.Msgtype_File;
		}
		return SessionInfo.Msgtype_Txt;
	}

	
	public String toString()
	{
		StringBuffer strBuf = new StringBuffer();
		String msgTypeStr = toMsgTypeDesc();
		strBuf.append(msgTypeStr).append(";");
		strBuf.append(Key_Attr).append("=").append(toSessionTypeDesc()).append(";");
		strBuf.append(Key_CType).append("=").append("orig").append(";");
		if(sessionType == SessionInfo.SessionType_Group)
		{
			strBuf.append(Key_GNum).append("=").append(senderId).append(";");
		}
		else
		{
			strBuf.append(Key_PNum).append("=").append(senderId).append(";");
		}
		
		return strBuf.toString();
	}
	

	public String toMsgTypeDesc()
	{
		if(this.msgType == SessionInfo.Msgtype_Txt)
		{
			return MsgType_Desc_SDS;
		}
		if(this.msgType == SessionInfo.Msgtype_File)
		{
			return MsgType_Desc_FD;
		}
		else 
		{
			return MsgType_Desc_SDS;
		}
	}
	
	public String toSessionTypeDesc()
	{
		if(sessionType == SessionInfo.SessionType_Group)
		{
			return SessionType_Desc_O2G;
		}
		if(sessionType == SessionInfo.SessionType_Single)
		{
			return SessionType_Desc_O2O;
		}
		else 
		{
			return SessionType_Desc_O2O;
		}
	}
}
